package cn.app.android.sichuanxiaoke.com.xiaokeapp.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;


import com.bumptech.glide.Glide;
import com.jude.rollviewpager.RollPagerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.R;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.activity.PayActivity;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.activity.RechargeActivity;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.adapter.MyGridViewAdapter;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.adapter.TestNormalAdapter;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.bean.Commodity;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.view.NonScrollGridView;

/**
 * Created by weian on 2017/8/18.
 */

public class HomeFragment extends Fragment implements View.OnClickListener{
    private RollPagerView mRollViewPager;
    private RelativeLayout mRe1;
    private RelativeLayout mRe2;
    private RelativeLayout mRe3;
    private RelativeLayout mRe4;
    private RelativeLayout mRe5;
    private RelativeLayout mRe6;
    private RelativeLayout mRe7;
    private RelativeLayout mRe8;
    private ImageView mImg1;
    private ImageView mImg2;
    private ImageView mImg3;
    private NonScrollGridView mMyGridView;
    private MyGridViewAdapter adapter;
    private EditText mSearchEtInput;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);
        ButterKnife.bind(this,view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mRollViewPager = (RollPagerView) getView().findViewById(R.id.roll_view_pager);
        mRe1 = (RelativeLayout) getView().findViewById(R.id.re_1);
        mRe2 = (RelativeLayout) getView().findViewById(R.id.re_2);
        mRe3 = (RelativeLayout) getView().findViewById(R.id.re_3);
        mRe4 = (RelativeLayout) getView().findViewById(R.id.re_4);
        mRe5 = (RelativeLayout) getView().findViewById(R.id.re_5);
        mRe6 = (RelativeLayout) getView().findViewById(R.id.re_6);
        mRe7 = (RelativeLayout) getView().findViewById(R.id.re_7);
        mRe8 = (RelativeLayout) getView().findViewById(R.id.re_8);
        mImg1 = (ImageView) getView().findViewById(R.id.img_1);
        mImg2 = (ImageView) getView().findViewById(R.id.img_2);
        mImg3 = (ImageView) getView().findViewById(R.id.img_3);
        mMyGridView = (NonScrollGridView) getView().findViewById(R.id.my_grid_view);
        mSearchEtInput = (EditText) getView().findViewById(R.id.search_et_input);
        mSearchEtInput.setFocusable(false);
        Drawable leftDrawable = mSearchEtInput.getCompoundDrawables()[0];
        if (leftDrawable != null) {
            leftDrawable.setBounds(0, 0, 70, 70);
            mSearchEtInput.setCompoundDrawables(leftDrawable, mSearchEtInput.getCompoundDrawables()[1], mSearchEtInput.getCompoundDrawables()[2], mSearchEtInput.getCompoundDrawables()[3]);
        }
        setImg();
        initData();
        mRe2.setOnClickListener(this);
    }

    private void setImg() {
        Glide.with(this).load("http://pic10.nipic.com/20101003/3101644_140400043414_2.jpg").into(mImg1);
        Glide.with(this).load("http://pic10.nipic.com/20101003/3101644_140400043414_2.jpg").into(mImg2);
        Glide.with(this).load("http://pic10.nipic.com/20101003/3101644_140400043414_2.jpg").into(mImg3);
    }

    private void initData() {
        List<Commodity> commodities = new ArrayList<>();
        commodities.add(new Commodity("http://pic10.nipic.com/20101003/3101644_140400043414_2.jpg", "茶叶1", "299"));
        commodities.add(new Commodity("http://pic28.nipic.com/20130425/2786001_140103647000_2.jpg", "茶叶2", "299"));
        commodities.add(new Commodity("http://pic28.nipic.com/20130425/2786001_140103647000_2.jpg", "茶叶2", "299"));
        commodities.add(new Commodity("http://pic.qiantucdn.com/58pic/13/92/99/05V58PICkuT_1024.jpg!" +
                "/fw/780/watermark/url/L3dhdGVybWFyay12MS4zLnBuZw==/align/center", "茶叶3", "299"));
        adapter = new MyGridViewAdapter(getContext(), commodities);
        mMyGridView.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        //设置播放时间间隔
        mRollViewPager.setPlayDelay(2000);
        //设置透明度
        mRollViewPager.setAnimationDurtion(500);
        //设置适配器
       // mRollViewPager.setAdapter(new TestNormalAdapter());
    }

    @Override
    public void onClick(View view) {
        if (view == mRe2){
            startActivity(new Intent(getActivity(), PayActivity.class));
        }
    }
    @OnClick({R.id.transfer_accounts})
    public  void OnClick(View view){
        switch (view.getId()){
            case R.id.transfer_accounts:
                startActivity(new Intent(getContext(), RechargeActivity.class));
                break;
        }
    }
}
